<?php
namespace Todo\Controller;
use Todo\Service\UserService;

/**
 * Class BackendController
 * 后台类应用基类控制器
 * @package Admin\Controller
 */
class BackendController extends BaseController {

    protected $_model = null;
    protected $_order = 'id DESC';
    protected $_list_url = '';
    protected $_limit = 20;

    /**
     * 初始化，完成配置加载， 用户确认，菜单加载
     */
    protected function _initialize(){
        $appInfo = D('Todo/Sys/Application')->findByField('url',MODULE_NAME);
        if(!$appInfo){
            $this->error("非法请求!");
        }
        $this->assign('_app', $appInfo);

        parent::_initialize();

        $this->_loadUser(true);

        $this->_list_url = I('post._current_url');
        if(!$this->_list_url) $this->_list_url = U('index');

        $appList = D('Todo/Sys/Application')->findAll();
        $this->assign('_appList', $appList);

        $menuList = UserService::getInstance()->getUserMenu($this->_user['id'], $appInfo['id']);
        $this->assign('_menuList', $menuList);

        $menuList = UserService::getInstance()->getTopMenu(array_column($menuList,'id'));
        $this->assign('_topMenu', $menuList);

        //当前菜单
        $menuId = I('get.menuId/d');
        if($menuId && $menuId != cookie('current_menu')) cookie('current_menu', $menuId);
    }


    /**
     * 加载模型类，需要重写
     */
    protected function _loadModel(){
        if(!$this->_model) $this->_model = D('Todo/'.MODULE_NAME.'/'.CONTROLLER_NAME);
    }


    /**
     * 默认查询条件
     * @return array
     */
    protected function _search(){
        $this->_loadModel();
        $map = array();
        foreach ( $this->_model->getDbFields () as $key => $val ) {
            if (isset ( $_REQUEST [$val] ) && $_REQUEST [$val] != '') {
                $map [$val] = $_REQUEST [$val];
            }
        }
        return $map;
    }

    /**
     * 自定义查询条件
     * @param array &$where 丢生成的查询条件
     */
    protected function _filter(&$where){}


}